package nsh.cl.nd.khtn.mazedash;

import nsh.cl.nd.khtn.mazedash.baseclasses.ShakeCamera;
import nsh.cl.nd.khtn.mazedash.baseclasses.SwipeDetector;
import nsh.cl.nd.khtn.mazedash.game.LoadingScene;
import nsh.cl.nd.khtn.mazedash.game.play.PlayScene;

import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import android.os.Bundle;
import android.view.KeyEvent;

public class RootGameActivity extends BaseGameActivity {
	private static RootGameActivity instance;
	private ShakeCamera camera;

	private SoundMusicManager soundHandler;
	private ScreenManager screenManager;
	private SwipeDetector swipeDetector;

	public static RootGameActivity getInstance() {
		return instance;
	}

	public ShakeCamera getCamera() {
		return camera;
	}

	public SoundMusicManager getSoundMusicManager() {
		return soundHandler;
	}

	public ScreenManager getScreenManager() {
		return screenManager;
	}
	
	@Override
	protected void onCreate(Bundle pSavedInstanceState) {
		super.onCreate(pSavedInstanceState);
		swipeDetector = new SwipeDetector(this);
	}

	@Override
	public EngineOptions onCreateEngineOptions() {
		camera = new ShakeCamera(0, 0, GameConstant.CAMERA_WIDTH, GameConstant.CAMERA_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR,
				new FillResolutionPolicy(), camera);
		engineOptions.getTouchOptions().setNeedsMultiTouch(GameConstant.IS_MULTITOUCH);
		engineOptions.getAudioOptions().setNeedsSound(true);
		engineOptions.getAudioOptions().getSoundOptions().setMaxSimultaneousStreams(1);
		engineOptions.getAudioOptions().setNeedsMusic(false);
		engineOptions.getTouchOptions().setTouchEventIntervalMilliseconds(1);
		return engineOptions;
	}

	@Override
	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
		instance = this;
		screenManager = new ScreenManager(mEngine);
		soundHandler = new SoundMusicManager();
		pOnCreateSceneCallback.onCreateSceneFinished(new LoadingScene().loadResources());
	}

	@Override
	public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (screenManager != null) {
			if (getEngine().getScene() instanceof PlayScene) {
				System.out.println("KEY UP!!!");
			}
		}

		return true;
	}
	
	public SwipeDetector getSwipeDetector() {
		return swipeDetector;
	}

	public void setSwipeDetector(SwipeDetector swipeDetector) {
		this.swipeDetector = swipeDetector;
	}
	
	
	/* 
	 * ===============================================================================================================================================
	 * EXTERNAL SERVICE (GOOGLE) below this description
	 * =============================================================================================================================================== 
	 */
	

//	/*
//	 * GOOGLE ADMOB HERE
//	 */
//	private AdView adView;
//
//	@Override
//	protected void onSetContentView() {
//		final FrameLayout frameLayout = new FrameLayout(this);
//		final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(
//				FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//		adView = new AdView(this);
//
//		adView.setAdUnitId(getResources().getString(R.string.ad_unit_id));
//		adView.setAdSize(AdSize.BANNER);
//
//		FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(
//				FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL
//						| Gravity.BOTTOM);
//		AdRequest.Builder builder = new AdRequest.Builder();
//		builder.addTestDevice("D7B094B0E0CE249499C2E8CFCC92FE10");
//		AdRequest adRequest = builder.build();
//		adView.loadAd(adRequest);
//
//		mRenderSurfaceView = new RenderSurfaceView(this);
//		mRenderSurfaceView.setRenderer(mEngine, this);
//
//		LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
//		frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
//		frameLayout.addView(adView, adViewLayoutParams);
//		setContentView(frameLayout, frameLayoutLayoutParams);
//
//		showAd();
//	}
//
//	public void showAd() {
//		runOnUiThread(new Runnable() {
//
//			@Override
//			public void run() {
//				adView.setVisibility(AdView.VISIBLE);
//			}
//		});
//	}
//
//	public void hideAd() {
//		runOnUiThread(new Runnable() {
//
//			@Override
//			public void run() {
//				adView.setVisibility(AdView.INVISIBLE);
//			}
//		});
//	}
//
//	/*
//	 * GOOGLE PLAY GAMES
//	 */
//	private GameHelper mHelper;
//
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		// create game helper with all APIs (Games, Plus, AppState):
//		mHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
//		GameHelperListener listener = new GameHelper.GameHelperListener() {
//			@Override
//			public void onSignInSucceeded() {
//				//instance.toastOnUIThread("SIGNIN OK");
//			}
//
//			@Override
//			public void onSignInFailed() {
//				//instance.toastOnUIThread("SIGNIN FAILED");
//			}
//
//		};
//		mHelper.setup(listener);
//	}
//
//	@Override
//	protected void onStart() {
//		super.onStart();
//		getGameServiceHelper().onStart(this);
//	}
//
//	@Override
//	protected void onStop() {
//		super.onStop();
//		getGameServiceHelper().onStop();
//	}
//
//	@Override
//	protected void onActivityResult(int request, int response, Intent data) {
//		super.onActivityResult(request, response, data);
//		getGameServiceHelper().onActivityResult(request, response, data);
//		if (GamesActivityResultCodes.RESULT_APP_MISCONFIGURED == response) {
//			// TODO AHO store this, this means the app is not support Google
//			// Play Games.
//		}
//	}
//
//	public GameHelper getGameServiceHelper() {
//		return mHelper;
//	}

}